package com.book.stat.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.book.common.annotation.ControllerEndpoint;
import com.book.common.annotation.Log;
import com.book.common.controller.BaseController;
import com.book.common.entity.FebsConstant;
import com.book.common.entity.FebsResponse;
import com.book.common.entity.QueryRequest;
import com.book.common.utils.FebsUtil;
import com.book.stat.entity.BreakageStat;
import com.book.common.utils.MonthAndTypeByCount;
import com.book.stat.service.BreakageStatService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class BreakageStatController extends BaseController {

    @Autowired
    BreakageStatService breakageStatService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "stat/breakageStat")
    public String breakageStat() {
        return FebsUtil.view("stats/breakageStat");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "breakage/breakageStat")
    public String breReturnIndex() {
        return FebsUtil.view("breakage/stats/breakageStats");
    }

    @GetMapping("breakageStat")
    @ResponseBody
    @RequiresPermissions("breakageStat:view")
    public FebsResponse findAccident(BreakageStat breakageStat) {
        return new FebsResponse().success().data(breakageStatService.statList(breakageStat));
    }

    @Log("访问图书报损统计表")
    @GetMapping("breakageStat/list")
    @ResponseBody
    @RequiresPermissions("breakageStat:view")
    public FebsResponse accidentList(QueryRequest request, BreakageStat breakageStat) {
        Map<String, Object> dataTable = getDataTable(this.breakageStatService.statIpPage(request, breakageStat));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * @Description: 柱状图
     * @Param: breakageStat
     * @return: object
     * @Author: Mr.Zhang
     * @Date: 2021/1/8
     */
    @GetMapping("breakageStat/column/{year}")
    @ResponseBody
    @ControllerEndpoint(operation = "柱状图成功渲染", exceptionMessage = "柱状图渲染失败")
    public FebsResponse getBreakageList(@PathVariable String year, BreakageStat breakageStat) {
        //查询所有 放入breakageStats
        List<BreakageStat> breakageStats = breakageStatService.statList(breakageStat);
        //符合条件的数据 breakageStatList1
        List<BreakageStat> breakageStatList1 = new ArrayList<>();
        //排除重复数据
        Set<BreakageStat> breakageStatSet = new HashSet<>();
        for (int i = 0; i < breakageStats.size(); i++) {
            //得到名字相同的书籍（可能为1，也可能多个）  放入breakageStatList
            QueryWrapper<BreakageStat> queryWrapper = new QueryWrapper<>();
            //已审批过的报损
            queryWrapper.eq("omit", "1");
            queryWrapper.eq("bookname", breakageStats.get(i).getBookname());
            queryWrapper.eq("year", year);
            List<BreakageStat> breakageStatList = breakageStatService.list(queryWrapper);
            //遍历名字不唯一的书籍
            if (breakageStatList.size() > 1) {
                int total = 0;
                for (int l = 0; l < breakageStatList.size(); l++) {
                    //得到相同名称的书籍数量
                    total = total + breakageStatList.get(l).getNumber();
                }
                //随便拿一条数据，将书籍总量赋值
                breakageStatList.get(0).setNumber(total);
                //放入符合条件的集合
                breakageStatList1.add(breakageStatList.get(0));
            } else {
                //直接拿到不相同书籍的数据
                breakageStatList1.add(breakageStats.get(i));
            }
        }
        for (int i = 0; i < breakageStatList1.size(); i++) {
            breakageStatSet.add(breakageStatList1.get(i));
        }
        return new FebsResponse().success().data(breakageStatSet);
    }

    /**
     * @Description: 折线图
     * @return: object
     * @Author: Mr.Zhang
     * @Date: 2021/1/8
     */
    @GetMapping("breakageStat/line/{year}")
    @ResponseBody
    @ControllerEndpoint(operation = "折线图成功渲染", exceptionMessage = "折线图渲染失败")
    public FebsResponse getBreakageCount(@PathVariable String year) {
        //月份
        String[] month = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        //报损类型
        Integer[] type = {1, 2, 3};
        //单月报损总数
        int total = 0;
        List<Integer[]> countList = new ArrayList<>();
        Integer[] count1 = new Integer[12];
        Integer[] count2 = new Integer[12];
        Integer[] count3 = new Integer[12];
        for (int i = 0; i < month.length; i++) {
            for (int t = 0; t < type.length; t++) {
                QueryWrapper<BreakageStat> queryWrapper = new QueryWrapper<>();
                //确定审审批状态
                queryWrapper.eq("omit", "1");
                //确定年份
                queryWrapper.eq("year", year);
                //确定月份
                queryWrapper.eq("month", month[i]);
                //确定类型
                queryWrapper.eq("bookfrom", type[t]);
                List<BreakageStat> breakageStatList = breakageStatService.list(queryWrapper);
                for (int b = 0; b < breakageStatList.size(); b++) {
                    total = total + breakageStatList.get(b).getNumber();
                }
                if (type[t] == 1) {
                    count1[i] = total;
                    total = 0;
                }
                if (type[t] == 2) {
                    count2[i] = total;
                    total = 0;
                }
                if (type[t] == 3) {
                    count3[i] = total;
                    total = 0;
                }
            }
        }
        countList.add(count1);
        countList.add(count2);
        countList.add(count3);
        return new FebsResponse().success().data(countList);
    }

    /**
     * @Description: 饼型图
     * @return: object
     * @Author: Mr.Zhang
     * @Date: 2021/1/8
     */
    @GetMapping("breakageStat/pctn/{year}")
    @ResponseBody
    @ControllerEndpoint(operation = "饼状图成功渲染", exceptionMessage = "饼状图渲染失败")
    public FebsResponse getBreakagePctn(@PathVariable String year) {
        QueryWrapper<BreakageStat> queryWrapper1 = new QueryWrapper<>();
        List<BreakageStat> normal = breakageStatService.list(queryWrapper1.eq("bookfrom", 1).eq("omit", "1").eq("year", year));
        QueryWrapper<BreakageStat> queryWrapper2 = new QueryWrapper<>();
        List<BreakageStat> back = breakageStatService.list(queryWrapper2.eq("bookfrom", 2).eq("omit", "1").eq("year", year));
        QueryWrapper<BreakageStat> queryWrapper3 = new QueryWrapper<>();
        List<BreakageStat> accident = breakageStatService.list(queryWrapper3.eq("bookfrom", 3).eq("omit", "1").eq("year", year));
        int normalTotal = 0;
        int backTotal = 0;
        int accidentTotal = 0;
        for (int i = 0; i < normal.size(); i++) {
            normalTotal = normalTotal + normal.get(i).getNumber();
        }
        for (int i = 0; i < back.size(); i++) {
            backTotal = backTotal + back.get(i).getNumber();
        }
        for (int i = 0; i < accident.size(); i++) {
            accidentTotal = accidentTotal + accident.get(i).getNumber();
        }
        MonthAndTypeByCount countN = new MonthAndTypeByCount("正常报损", normalTotal);
        MonthAndTypeByCount countB = new MonthAndTypeByCount("退回报损", backTotal);
        MonthAndTypeByCount countA = new MonthAndTypeByCount("意外报损", accidentTotal);
        List<MonthAndTypeByCount> countList = new ArrayList<>();
        countList.add(countN);
        countList.add(countB);
        countList.add(countA);
        return new FebsResponse().success().data(countList);
    }
}
